{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 单分支语句 if"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 less than 2\n"
     ]
    }
   ],
   "source": [
    "if 1 < 2 : # if写完必须冒号，语句块必须缩紧不然语法错误\n",
    "    print(\"1 less than 2\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 less than 2\n"
     ]
    }
   ],
   "source": [
    "if 1 > 2 :\n",
    "    print (\"1 geater than 2\")\n",
    "else:\n",
    "    print (\"1 less than 2\")  #所有的分支结构都只能进一个分支，如果if满足，就不会进else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "good\n"
     ]
    }
   ],
   "source": [
    "score = 80              # 判断的条件是从上向下判断，有一个条件满足就会输出结果。\n",
    "if score >= 60 :       # 如果不用else可能会出现逻辑漏洞，有的漏洞是错误，有的是故意留的可能有用\n",
    "     print(\"good\")     # 注意 if 后面写完必须加  ：\n",
    "elif score > 100:\n",
    "     print(\"too big\")\n",
    "elif score > 0 :\n",
    "     print (\"not good\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "too big\n"
     ]
    }
   ],
   "source": [
    "score = 80                  \n",
    "if score > 100 :         # 判断过这个分支后会自带一个隐含的条件，score<=100，无需在下面继续标注。\n",
    "     print(\"good\")          \n",
    "elif score >= 60:\n",
    "     print(\"too big\")\n",
    "elif score > 0 :\n",
    "     print (\"not good\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "too big\n"
     ]
    }
   ],
   "source": [
    "score = 80                  # 此种为嵌套结构，嵌套结构必须先写if\n",
    "if score > 100 :            \n",
    "     print(\"good\")\n",
    "else:\n",
    "    if score >= 60:\n",
    "        print(\"too big\")\n",
    "    elif score > 0 :\n",
    "        print (\"not good\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "good\n"
     ]
    }
   ],
   "source": [
    "score = 80        # if中往往会写比较或者逻辑运算符，如果写恒定的，都等于 True，永远打印此行\n",
    "if 1 :                     \n",
    "     print(\"good\")\n",
    "else:\n",
    "    if score >= 60:\n",
    "        print(\"too big\")\n",
    "    elif score > 0 :\n",
    "        print (\"not good\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n"
     ]
    }
   ],
   "source": [
    "               # 0 = \"\" = {] = () = set = false 只有空的才会false，等效false见真值表\n",
    "if True :    # if 后面的如果恒等于True，那么会一直打印,\n",
    "    print(\"true\")    # 此处详细的真值表见 TY笔记\n",
    "else : \n",
    "    print (\"false\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">>123\n"
     ]
    }
   ],
   "source": [
    "a = input (\">>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = int (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "124"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(float(\"10.5\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '10.5'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-18-54dd49a25c21>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"10.5\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: '10.5'"
     ]
    }
   ],
   "source": [
    "int(\"10.5\")#int不能转出两层变化，只能一层一层的转，所以要先加一层转换为浮点型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w12345\n"
     ]
    }
   ],
   "source": [
    "a=input (\"w\")   #主要掌握 input 函数怎么用和内建函数 int 怎么用，\n",
    "                # input函数 可以随时修改内容，交互式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "b = int (a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w124\n"
     ]
    }
   ],
   "source": [
    "c = input (\"w\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = int (c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "124"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b > d\n"
     ]
    }
   ],
   "source": [
    "if b>d:\n",
    "    print(\"b > d\")   # print（）括号里面要加双引号才能打印出print的内容，这个是数字比较。\n",
    "elif b == d:\n",
    "    print(\"b == d\")\n",
    "else:\n",
    "    print(\"b < d\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('12345', '124')"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a,c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a < c\n"
     ]
    }
   ],
   "source": [
    "if a > c:                 # 字符串的比较为  12345\n",
    "    print (\"a > c\")       #                 124\n",
    "else:                     #          对齐比较到 4 > 3 ,就不再比较了\n",
    "    print (\"a < c\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">=5\n"
     ]
    }
   ],
   "source": [
    "z = 12345\n",
    "if z >=10000:\n",
    "    print (\">=5\")\n",
    "elif z >=1000:  # if 之后的判断用elif，不然就会一直判断，\n",
    "    print (\"4\")  # 每一个if相当于新建一个分支，打印的结果就会有多个\n",
    "elif z >=100:\n",
    "    print (\"3\")\n",
    "elif z >=10:\n",
    "    print (\"2\")\n",
    "else:\n",
    "    print:(\"1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">=5\n"
     ]
    }
   ],
   "source": [
    "z = 12345            # 一到五位的正整数，判断有几位\n",
    "if z >=1000:        # 此种类型叫做折半，可以大大提高效率，\n",
    "    if z >=10000:   # 折半的前提是有顺序的，不然折半也是么有意义的\n",
    "        print (\">=5\")\n",
    "    else:\n",
    "        print (\"4\")\n",
    "else:\n",
    "    if z >=100:\n",
    "        print (\"3\")\n",
    "    elif z >=10:\n",
    "        print (\"2\")\n",
    "    else:\n",
    "        print (\"1\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
